package com.jzg.jcptadmin.presenter;


import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.ResponseJson;
import com.jzg.jcptadmin.data.vo.AdmixedData;
import com.jzg.jcptadmin.viewinterface.IAdmixedInf;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by 李波 on 2017/3/17.
 * 车辆参配
 */

public class CarParamConfigPresenter extends BasePresenter<IAdmixedInf> {

    public CarParamConfigPresenter(DataManager dataManager) {
        super();
        this.dataManager = dataManager;
    }

    private DataManager dataManager;
    private IAdmixedInf mView;
    private Subscription mSubscription;


    @Override
    public void attachView(IAdmixedInf mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;

    }
    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }


    /**
     * Created by 李波 on 2017/3/17.
     * 获取车辆参配信息
     */
    public void requestAdmixedData(){
        checkViewAttached();
        if(!AppContext.isNetWork){
            mView.showError(Constant.errr_net);
            return;
        }
        mSubscription = dataManager.getCarParamConfig(mView.getParams())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io()).subscribe(new Observer<ResponseJson<AdmixedData>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if(e!=null&& mView!=null){
                            mView.showError(e.toString());
                        }
                    }

                    @Override
                    public void onNext(ResponseJson<AdmixedData> responseJson) {
                        AdmixedData admixedData = responseJson.getData();
                        mView.succeed(admixedData);
                    }
                });
    }

}